/*
-----------------------------------------------------------------------------
This source file is part of Labor3D
(Labor 3D Graphics Engine)

Copyright (c) 2011-2020 Rock Mao
* creator : Rock Mao
* created : 2011-10-15   22:29
* filename: LaborD3D112DRectRO.cpp
-----------------------------------------------------------------------------
*/
#include "stdafx.h"
#include "LaborD3D112DRectRO.h"
#include "LaborCore.h"
#include "Renderer/LaborRenderSystem.h"
NAMESPACE_LABOR3D_BEGIN

//---------------------------------------------------------------------
CLD3D112DRectRO::CLD3D112DRectRO()
{
	m_pTexture = NULL;
	m_pIndexBuffer = NULL;
	m_pVertexBuffer = NULL;
	m_RectSrc.d_Left = 0.0f;
	m_RectSrc.d_Top  = 0.0f;
	m_RectSrc.d_Right = 1.0f;
	m_RectSrc.d_Bottom = 1.0f;
	m_Color = CLColor_4f(1.0f,1.0f,1.0f,1.0f);
	init();
}

//---------------------------------------------------------------------
CLD3D112DRectRO::~CLD3D112DRectRO()
{
	//SafeDelete(m_pTexture);
	SafeDelete(m_pIndexBuffer);
	SafeDelete(m_pVertexBuffer);

}

//---------------------------------------------------------------------
void CLD3D112DRectRO::init()
{
	_calVertexData();
	LD3D102DVertex *pVertex = NULL;
	int vertexNumber = 4;
	m_pVertexBuffer = GetRenderSystem()->createVertexBuffer( sizeof(LD3D102DVertex), vertexNumber, HBU_STATIC);
	pVertex = (LD3D102DVertex*)m_pVertexBuffer->lock( 0, vertexNumber, eHBL_WRITE_DISCARD);
	for ( int i=0; i< vertexNumber; i++ )
	{
		*pVertex = m_Vertex[i];
		pVertex++;
	}
	m_pVertexBuffer->unlock();

	unsigned int indices[6] =
	{
		0  ,2 ,1 , 
		2  ,0 ,3 , 
	};

	//unsigned int indices[6] =
	//{
	//	1  ,0 ,3 , 
	//	2  ,1 ,3 , 
	//};

	m_pIndexBuffer  = GetRenderSystem()->createIndexBuffer( IT_32BIT, 6, HBU_WRITE_ONLY);

	int *pIndex = NULL;
	pIndex = static_cast<int*>(m_pIndexBuffer->lock(0, 6*sizeof(int), eHBL_WRITE_DISCARD));

	for ( int i=0; i< 6; i++ )
	{
		*pIndex = indices[i];
		pIndex++;
	}
	m_pIndexBuffer->unlock();
	m_bNeedUpdate = true;
}

//---------------------------------------------------------------------
void CLD3D112DRectRO::setDestRect( CLRectF & rect )
{
	m_RectDestLB.x = rect.d_Left ;
	m_RectDestLB.y = rect.d_Bottom;
	m_RectDestLB.z = GetRenderSystem()->get2DZValue() ;
	m_RectDestLB.w = 1.0f;

	m_RectDestRT.x = rect.d_Right;
	m_RectDestRT.y = rect.d_Top;
	m_RectDestRT.z = GetRenderSystem()->get2DZValue();
	m_RectDestRT.w = 1.0f;
	m_bNeedUpdate = true;
}

//---------------------------------------------------------------------
void CLD3D112DRectRO::setColor( CLColor_4f& _color )
{
	m_Color = _color;
	m_bNeedUpdate = true;
}

//---------------------------------------------------------------------
void CLD3D112DRectRO::setTexture( CLTexture *pTexture, int iLayer )
{
	if ( NULL == pTexture )
		return;
	m_pTexture = pTexture;
	m_RectSrc.d_Left = 0.0f;
	m_RectSrc.d_Top  = 0.0f;
	m_RectSrc.d_Right = 1.0f;
	m_RectSrc.d_Bottom = 1.0f;
	m_bNeedUpdate = true;
}

//---------------------------------------------------------------------
void CLD3D112DRectRO::setTexture( CLTexture *pTexture, CLRectF & srcRect, int iLayer )
{
	if ( NULL == pTexture )
		return;
	m_pTexture = pTexture;
	int _height;
	int _width;
	m_pTexture->getImageSize( _width, _height );
	
	m_RectSrc.d_Left = srcRect.d_Left / _width;
	m_RectSrc.d_Top  = srcRect.d_Top / _height;
	m_RectSrc.d_Right = srcRect.d_Right / _width;
	m_RectSrc.d_Bottom = srcRect.d_Bottom / _height;
	m_bNeedUpdate = true;
}

//---------------------------------------------------------------------
void CLD3D112DRectRO::draw()
{
	UINT stride = sizeof( LD3D102DVertex );
	UINT offset = 0;
	GetRenderSystem()->setVertexBuffer( 0,1, m_pVertexBuffer, &stride, &offset);
	GetRenderSystem()->setIndexBuffer( m_pIndexBuffer, IT_32BIT );
	GetRenderSystem()->setPrimitiveTopology( eL_PT_TRIANGLESTRIP );

	GetRenderSystem()->drawIndexed(6, 0, 0 );
}

//---------------------------------------------------------------------
void CLD3D112DRectRO::apply()
{
	if ( !m_bNeedUpdate )
		return;

	_calVertexData();
	m_pVertexBuffer->writeData( 0, sizeof(LD3D102DVertex) * 4, m_Vertex, true );

	m_bNeedUpdate = false;
}

//---------------------------------------------------------------------
void CLD3D112DRectRO::_calVertexData()
{
	CLPoint2DF _ptUVLB, _ptUVRT;
	_calTexUV( _ptUVLB, _ptUVRT );

	//m_Vertex[0].m_color = m_Color;
	m_Vertex[0].m_pos   = CLPoint4(m_RectDestLB.x , m_RectDestLB.y , m_RectDestLB.z , 1.0f);
	m_Vertex[0].m_uv    = _ptUVLB;

	//m_Vertex[1].m_color = m_Color;
	m_Vertex[1].m_pos   = CLPoint4(m_RectDestLB.x , m_RectDestRT.y , m_RectDestLB.z , 1.0f);
	m_Vertex[1].m_uv    = CLPoint2DF(_ptUVLB.x, _ptUVRT.y);


	//m_Vertex[2].m_color = m_Color;
	m_Vertex[2].m_pos   = CLPoint4(m_RectDestRT.x , m_RectDestRT.y , m_RectDestRT.z , 1.0f);
	m_Vertex[2].m_uv    = _ptUVRT;


	//m_Vertex[3].m_color = m_Color;
	m_Vertex[3].m_pos   = CLPoint4(m_RectDestRT.x , m_RectDestLB.y , m_RectDestRT.z , 1.0f);
	m_Vertex[3].m_uv    =  CLPoint2DF(_ptUVRT.x, _ptUVLB.y);

}

//---------------------------------------------------------------------
void CLD3D112DRectRO::_calTexUV(CLPoint2DF & uvLB, CLPoint2DF& uvRT)
{
	uvLB.x = m_RectSrc.d_Left;
	uvLB.y = m_RectSrc.d_Bottom;

	uvRT.x = m_RectSrc.d_Right;
	uvRT.y = m_RectSrc.d_Top;
}

NAMESPACE_LABOR3D_END